package com.sinoglobal.ningxia.activity.amusement;

import java.util.ArrayList;

import android.os.Bundle;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullGridView;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.IBase;
import com.sinoglobal.ningxia.adapter.PhotoWallAdapter;
import com.sinoglobal.ningxia.beans.PhotoInfoVo;
import com.sinoglobal.ningxia.beans.PhotoWallVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.AbstractActivity;
import com.sinoglobal.ningxia.http.Code;
/**
 * 照片墙
 * @author hxp
 * @createdate 2014-6-30 上午9:54:45
 * @Description: 
 */
public class PhotoWallActivity extends AbstractActivity implements IBase,AbOnListViewListener{
	private AbPullGridView photoPull;//照片墙
	private ArrayList<PhotoInfoVo> photoList;//数据
	private int pageNum = 0;//页数
	private String shopId;//商家id
	private PhotoWallAdapter adapter;//适配器
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo_wall);
		init();
		showListener();
		loadPhotoes(pageNum);
	}

    //设置布局值变量
	private void setData(PhotoWallVo result) {
		if(pageNum == 0){
			photoList = result.getList();
			photoPull.stopRefresh();
		}else{
			photoList.addAll(result.getList());
			photoPull.stopLoadMore();
		}
		if(null != photoList){
			adapter.setData(photoList);
		}
	}


	@Override
	public void init() {
		shopId = getIntent().getStringExtra(BusinessDetailActivity.SHOPID);
		pageNum = 0 ;
		photoPull = (AbPullGridView) findViewById(R.id.photo_list);
		photoPull.getHeaderView().setHeaderProgressBarDrawable(this.getResources().getDrawable(R.drawable.progress_circular));
		photoPull.getFooterView().setFooterProgressBarDrawable(this.getResources().getDrawable(R.drawable.progress_circular));
		photoPull.setPullLoadEnable(true);
		photoPull.setPullRefreshEnable(true);
		photoList = new ArrayList<PhotoInfoVo>();
		adapter = new PhotoWallAdapter(this,photoList);
		photoPull.setAdapter(adapter);
		titleView.setText(getString(R.string.photos_wall));
	}
	
	private void loadPhotoes(int page){
		new ItktAsyncTaskWithDialog<String, Void, PhotoWallVo>(getString(R.string.loading_img),true,true) {

			@Override
			public PhotoWallVo before(String... params) throws Exception {
				return RemoteImpl.getInstance().getPhotoWall(params[0], shopId);
			}

			@Override
			public void after(PhotoWallVo result) {
				if(null == result){
					return;
				}
				if(Code.CODE_SUCCESS==result.getCode()){
					setData(result);
				}
			}

			@Override
			public void exception() {
			}
		}.execute(String.valueOf(page));
	}

	@Override
	public void showListener() {
		photoPull.setAbOnListViewListener(this);
	}


	@Override
	public void onRefresh() {
		pageNum = 0;
		loadPhotoes(pageNum);
	}

	@Override
	public void onLoadMore() {
		loadPhotoes(++pageNum);
		
	}
}